package com.scada.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.scada.hessian.client.impl.RemindVisitorClient;
import com.scada.model.dataTable.MakeOverExpireList;
import com.scada.model.dataTable.MemberRetrunList;
import com.scada.model.dataTable.MemberReturnExpireList;
import com.scada.model.dataTable.PlanBuyExpireList;
import com.scada.model.dataTable.PlanBuyReturnList;
import com.scada.model.dataTable.ServiceExpireList;
import com.scada.model.result.PageInfo;
import com.scada.model.result.ResponseBean;
import com.scada.model.search.MakeOverExpireSearch;
import com.scada.model.search.MemberRetrunSearch;
import com.scada.model.search.MemberReturnExpireSearch;
import com.scada.model.search.PlanBuyExpireSearch;
import com.scada.model.search.PlanBuyReturnSearch;
import com.scada.model.search.ServiceExpireSearch;

@Controller
@RequestMapping("home")
public class HomeController extends BaseController{

	@Autowired
	private RemindVisitorClient remindVisitorClient;
	
	@RequestMapping(value = "getServiceExpirePageData")
	@ResponseBody
	public String getServiceExpirePageData(@ModelAttribute ServiceExpireSearch serviceExpireSearch) {
		
		PageInfo<ServiceExpireList> pageInfo = new PageInfo<ServiceExpireList>();
        pageInfo.setStart(serviceExpireSearch.getStart());
        pageInfo.setPageSize(serviceExpireSearch.getLength());
        pageInfo.setDraw(serviceExpireSearch.getDraw());
        
        serviceExpireSearch.setBeforeExpireNum(0);
        serviceExpireSearch.setAfterExpireNum(0);
        
        ResponseBean rb = remindVisitorClient.getServiceExpirePageData(serviceExpireSearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<ServiceExpireList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<ServiceExpireList>());
        }
        
		return toJson(pageInfo);
	}
	
	@RequestMapping(value = "getPlanBuyExpirePageData")
	@ResponseBody
	public String getPlanBuyExpirePageData(@ModelAttribute PlanBuyExpireSearch planBuyExpireSearch) {
		
		PageInfo<PlanBuyExpireList> pageInfo = new PageInfo<PlanBuyExpireList>();
        pageInfo.setStart(planBuyExpireSearch.getStart());
        pageInfo.setPageSize(planBuyExpireSearch.getLength());
        pageInfo.setDraw(planBuyExpireSearch.getDraw());
        
        planBuyExpireSearch.setBeforeExpireNum(0);
        planBuyExpireSearch.setAfterExpireNum(0);
        
        ResponseBean rb = remindVisitorClient.getPlanBuyExpirePageData(planBuyExpireSearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<PlanBuyExpireList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<PlanBuyExpireList>());
        }
        
		return toJson(pageInfo);
	}
	
	@RequestMapping(value = "toReturnTimeForm")
	public String toReturnTimeForm(Long memberId, ModelMap model){
		
		model.put("memberId", memberId);
		return "html/retrunTimeEdit";
	}
	
	@RequestMapping(value = "getPlanBuyReturnPageData")
	@ResponseBody
	public String getPlanBuyReturnPageData(@ModelAttribute PlanBuyReturnSearch planBuyReturnSearch) {
		
		PageInfo<PlanBuyReturnList> pageInfo = new PageInfo<PlanBuyReturnList>();
        pageInfo.setStart(planBuyReturnSearch.getStart());
        pageInfo.setPageSize(planBuyReturnSearch.getLength());
        pageInfo.setDraw(planBuyReturnSearch.getDraw());
        
        
        ResponseBean rb = remindVisitorClient.getPlanBuyReturnPageData(planBuyReturnSearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<PlanBuyReturnList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<PlanBuyReturnList>());
        }
        
		return toJson(pageInfo);
	}
	
	@RequestMapping(value = "getMRExpirePageData")
	@ResponseBody
	public String getMRExpirePageData(@ModelAttribute MemberReturnExpireSearch memberReturnExpireSearch) {
		
		PageInfo<MemberReturnExpireList> pageInfo = new PageInfo<MemberReturnExpireList>();
        pageInfo.setStart(memberReturnExpireSearch.getStart());
        pageInfo.setPageSize(memberReturnExpireSearch.getLength());
        pageInfo.setDraw(memberReturnExpireSearch.getDraw());
        
        memberReturnExpireSearch.setBeforeExpireNum(0);
        memberReturnExpireSearch.setAfterExpireNum(0);
        
        ResponseBean rb = remindVisitorClient.getMRExpirePageData(memberReturnExpireSearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<MemberReturnExpireList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<MemberReturnExpireList>());
        }
        
		return toJson(pageInfo);
	}
	
	@RequestMapping(value = "getMemberRetrunPageData")
	@ResponseBody
	public String getMemberRetrunPageData(@ModelAttribute MemberRetrunSearch memberRetrunSearch) {
		
		PageInfo<MemberRetrunList> pageInfo = new PageInfo<MemberRetrunList>();
        pageInfo.setStart(memberRetrunSearch.getStart());
        pageInfo.setPageSize(memberRetrunSearch.getLength());
        pageInfo.setDraw(memberRetrunSearch.getDraw());
        
        
        ResponseBean rb = remindVisitorClient.getMemberReturnPageData(memberRetrunSearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<MemberRetrunList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<MemberRetrunList>());
        }
        
		return toJson(pageInfo);
	}
	
	@RequestMapping(value = "getMakeOverExpirePageData")
	@ResponseBody
	public String getMakeOverExpirePageData(@ModelAttribute MakeOverExpireSearch makeOverExpireSearch) {
		
		PageInfo<MakeOverExpireList> pageInfo = new PageInfo<MakeOverExpireList>();
        pageInfo.setStart(makeOverExpireSearch.getStart());
        pageInfo.setPageSize(makeOverExpireSearch.getLength());
        pageInfo.setDraw(makeOverExpireSearch.getDraw());
        
        makeOverExpireSearch.setCycle(0);
        
        ResponseBean rb = remindVisitorClient.MakeOverExpirePageData(makeOverExpireSearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<MakeOverExpireList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<MakeOverExpireList>());
        }
        
		return toJson(pageInfo);
	}
}
